<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column label="项目名称" align="center">
        <template slot-scope="{row}">
          {{ row.projectName }}
        </template>
      </el-table-column>
      <el-table-column label="发布方" align="center">
        <template slot-scope="{row}">
          {{ row.employerName }}
        </template>
      </el-table-column>
      <el-table-column label="承接方" align="center">
        <template slot-scope="{row}">
          {{ row.contractorName }}
        </template>
      </el-table-column>
      <el-table-column label="纠纷类型" align="center">
        <template slot-scope="{row}">
          {{ row.categoryName }}
        </template>
      </el-table-column>
      <el-table-column label="投诉方" align="center">
        <template slot-scope="{row}">
          {{ row.type===1?'发布方投诉':'承接方投诉' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | styleFilter()">{{ row.status | statusFilter() }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handleDetail(row)">详情</el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">调解结果</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchProjectDisputeList" />

    <el-dialog title="项目纠纷调解" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="projectDisputeData" label-position="left" label-width="90px">
        <el-form-item label="项目名称">
          {{ projectDisputeData.projectName }}
        </el-form-item>
        <el-form-item label="发布方">{{ projectDisputeData.employerName }}</el-form-item>
        <el-form-item label="承接方">{{ projectDisputeData.contractorName }}</el-form-item>
        <el-form-item label="纠纷类型">{{ projectDisputeData.categoryName }}</el-form-item>
        <el-form-item label="投诉内容">
          {{ projectDisputeData.content }}
        </el-form-item>
        <el-form-item label="投诉方">{{ projectDisputeData.type===1?'项目发布方':'项目承接方' }}</el-form-item>
        <!-- <el-form-item label="状态">
          <el-tag :type="projectDisputeData.status | styleFilter()">{{ projectDisputeData.status | statusFilter() }}</el-tag>
        </el-form-item> -->
        <el-form-item label="创建时间">
          <span>{{ projectDisputeData.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </el-form-item>
        <el-form-item label="解决结果">
          <el-select v-model="projectDisputeData.status" placeholder="选择解决结果" clearable>
            <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="解决方案">
          <el-input v-model="projectDisputeData.solution" type="textarea" rows="5" placeholder="请填写解决方案" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateProjectDispute()">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="dialogDetailVisible">
      <el-form ref="dataForm" :model="projectDisputeData" label-position="left" label-width="110px">
        <el-form-item label="项目名称">
          {{ projectDisputeData.projectName }}
        </el-form-item>
        <el-form-item label="发布方">{{ projectDisputeData.employerName }}</el-form-item>
        <el-form-item label="承接方">{{ projectDisputeData.contractorName }}</el-form-item>
        <el-form-item label="纠纷类型">{{ projectDisputeData.categoryName }}</el-form-item>
        <el-form-item label="投诉内容">
          {{ projectDisputeData.content }}
        </el-form-item>
        <el-form-item label="投诉方">{{ projectDisputeData.type===1?'项目发布方':'项目承接方' }}</el-form-item>
        <el-form-item v-if="projectDisputeData.solution" label="解决方案">
          <span>{{ projectDisputeData.solution }}</span>
        </el-form-item>
        <el-form-item label="投诉方">
          <span>{{ projectDisputeData.type===1?'项目发布方':'项目承接方' }}</span>
        </el-form-item>
        <el-form-item v-if="projectDisputeData.soluveTime" label="纠纷解决时间">
          <span>{{ projectDisputeData.soluveTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-tag :type="projectDisputeData.status | styleFilter()">{{ projectDisputeData.status | statusFilter() }}</el-tag>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ projectDisputeData.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectDisputeList, editProjectDispute, editProjectDisputeStatus } from '@/api/ProjectDispute'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  name: 'ProjectDispute',
  components: {
    Pagination
  },
  filters: {
    styleFilter(style) {
      const styleMap = { 1: 'warning', 2: 'success' }
      return styleMap[style]
    },
    timeFilter(time, format) {
      return parseTime(time, format)
    },
    statusFilter(value) {
      const statusMap = { 1: '待解决', 2: '已解决', 3: '终止合作' }
      return statusMap[value]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      statusOptions: [
        { key: 1, statusName: '待解决' },
        { key: 2, statusName: '已解决' },
        { key: 3, statusName: '终止合作' }
      ],
      projectDisputeData: {},
      dialogFormVisible: false,
      dialogDetailVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      rules: {}
    }
  },
  created() {
    this.fetchProjectDisputeList()
  },
  methods: {
    fetchProjectDisputeList() {
      this.listLoading = true
      getProjectDisputeList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchProjectDisputeList()
    },
    resetProjectDisputeData() {
      this.projectDisputeData = {}
    },
    handleDetail(row) {
      this.projectDisputeData = Object.assign({}, row)
      this.dialogDetailVisible = true
    },
    handleUpdate(row) {
      this.projectDisputeData = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateProjectDispute() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.projectDisputeData)
          editProjectDispute(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.projectDisputeData.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.projectDisputeData)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    delProjectDispute(row) {
      row.status = 0
      this.updateProjectDisputeStatus(row, 0)
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    reuseProjectDispute(row) {
      row.status = 1
      this.updateProjectDisputeStatus(row, 1)
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    updateProjectDisputeStatus(row, status) {
      row.status = status
      editProjectDisputeStatus(row.id, status).then(() => {
        this.projectDisputeData = Object.assign({}, row)
        this.projectDisputeData.status = status
        for (const v of this.list) {
          if (v.id === this.projectDisputeData.id) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1, this.projectDisputeData)
            break
          }
        }
      })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;
  .filter-container {
    padding: 10px 0;
    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }
}
</style>
